package com.aaa.market.service.impl;


import com.aaa.market.mapper.PositionMapper;
import com.aaa.market.pojo.entity.Position;
import com.aaa.market.pojo.vo.PositionVo;
import com.aaa.market.service.PositionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gsj.common.pojo.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: clms-eighth
 * @description:
 * @author: 刘守霖
 * @create: 2021-07-25 15:07
 **/
@Service
public class PositionServiceIpml implements PositionService {
    @Autowired
    private PositionMapper positionMapper;
    @Override
    public R findPositionByPage(Integer currentPage, Integer pageSize, PositionVo positionVo) {
        Page<Position> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Position> wrapper=new QueryWrapper<>();
        if (StringUtils.isNotEmpty(positionVo.getPositionName())){
            wrapper.like("position_name",positionVo.getPositionName());
        }
        Page<Position> page1 = positionMapper.selectPage(page, wrapper);
        return R.ok().put("records",page1.getRecords()).put("total",page1.getTotal());
    }

    @Override
    public R insertPosition(Position position) {
        String name = position.getPositionName();
        List<Position> selectAll = positionMapper.selectAll();
        for (Position position1:selectAll){
            if (position1.getPositionName().equals(name)){
                return R.ok().put("message","名字已重复");
            }
        }
        positionMapper.insertPositionTable(position);
        return R.ok().put("message","新增成功");
    }

    @Override
    public R chaXun(Integer id) {
        List<Position> positions = positionMapper.chaXun(id);

        return R.ok().put("positions",positions);
    }
    @Override
    public R changePosition(Position position) {
//        String name = position.getName();
//        List<Position> selectAll = positionMapper.selectAll();
//        for (Position position1:selectAll){
//            if (position1.getName().equals(name)){
//                return R.ok().put("message","名字已重复");
//            }
//        }
        positionMapper.changePosition(position);
        return R.ok().put("message","修改成功");
    }

    @Override
    public R delPosition(Integer id) {
        positionMapper.delPosition(id);
        return null;
    }

    @Override
    public R queryPosition() {
        List<Position> positions = positionMapper.selectAll();
        return R.ok().put("positions",positions);
    }

}
